<?php 
    //计算昨天的访客平均驻留时间
	//设置时区
	ini_set('date.timezone','Asia/Shanghai');
    $conn = new mysqli("localhost:3306","root","lc971019");
	if($conn->connect_error)
	{
		die('Could not connect:'.$conn->connect_error());
	}
	
	//昨天上午8点开始计算
	$time_start = date("Y-m-d",intval(time()))." 08:00:00";
	$time_start = date("Y-m-d H:i:s",strtotime($time_start)-3600*24);
	//昨天晚上22点
	$time_end = date("Y-m-d",intval(time()))." 22:00:00";
	$time_end = date("Y-m-d H:i:s",strtotime($time_end)-3600*24);
	
	
	$sql="select min(times), max(times) from wifidata where times > ".$time_start." and times < ".$time_end." and ranges < 200 group by mac;";
	
	$result_staytime_yesterday = $conn->query($sql);
	
	$StayTime_yesterday_time=0;//驻留总时间
	$i=0;//总人数
	$out=0;//跳出人数
	$in=0;//深访人数
	if($result_staytime_yesterday->num_rows > 0){
		while($row = $result_staytime_yesterday->fetch_assoc()){
			$StayTime_yesterday_time=$StayTime_yesterday_time+strtotime($row["max(times)"])-strtotime($row[" min(times)"]);
			$i=$i+1;
			//小于5分钟则跳出人数加1
			if((strtotime($row["max(times)"])-strtotime($row[" min(times)"]))<=5*60){
				$out=$out+1;
				}
			//大于30分钟则深访人数加1
			if((strtotime($row["max(times)"])-strtotime($row[" min(times)"]))>30*60){
				$in=$in+1;
				}
			}
		}
	$StayTime_avg=intval($StayTime_yesterday_time/$i);
	$days = intval($StayTime_avg/86400);
	$remain = $StayTime_avg%86400;
	$hours = intval($remain/3600);
	$remain = $remain%3600;
    $mins = intval($remain/60);
	//平均驻留时间
	$staytime=$days."天 ".$hours."小时 ".$mins."分钟";
	//跳出率
	$outrate=round($out/$i,2);
	//深访率
	$inrate=round($in/$i,2);
?>